package controller;

import entity.User;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet(name = "LoginServlet" ,urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("username");
        String password=request.getParameter("password");
        User user=new User();
        user.setName(userName);
        user.setPass(password);
        if (userName==""||userName==null){
            //注册失败,跳会注册页面
            String msg="注册失败";
            //重定向
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }else{
            //注册成功,调回登录页面
            request.getSession().setAttribute("user",user);
            request.getRequestDispatcher(request.getContextPath()+"/newsServlet?action=select").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request,response);
    }
}
